package com.uwang.library.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.taglibs.standard.extra.spath.ParseException;

import com.alibaba.fastjson.JSON;
import com.uwang.library.model.BtnModel;
import com.uwang.library.model.StatisticalDataModel;
import com.uwang.library.service.BtnService;
import com.uwang.library.service.impl.BtnServiceImpl;
import com.uwang.library.util.KcMadeException;

@WebServlet("/btn")
public class BtnServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		BtnService btnService = new BtnServiceImpl();
		if("bookId".equals(req.getParameter("commond"))) {
			// 数据统计查询
			int bookId = Integer.parseInt(req.getParameter("book_id"));
			btnService.bookReturn(bookId);
		}else if("tongji".equals(req.getParameter("commond"))) {
			// 数据统计查询
			StatisticalDataModel statisticalDataModelList = btnService.AllData();
			resp.setContentType("text/html; charset=utf-8");
			PrintWriter out1 = resp.getWriter();
			// 将JAVA对象给fastjson转换
			String json1 = JSON.toJSONString(statisticalDataModelList);
			out1.write(json1);
			out1.flush();
			out1.close();
		}else {
			//借阅书籍的ID值
			int bookId = Integer.parseInt(req.getParameter("book_id"));
			//传入的时间戳
			Long term = (Long.valueOf(req.getParameter("term")))*(1000*60);
			//会员手机号码
			String userPhone = req.getParameter("userPhone");
			//查询是否借阅
			BtnModel btnList = btnService.findAllMenu(bookId);
			System.out.println(btnList);
			int bookNum = btnService.queryId(bookId);
			int userPhoneNum = btnService.queryPhone(userPhone);
			if (null == btnList.getId() && bookNum > 0  && userPhoneNum >0) {
				PrintWriter out = resp.getWriter();
				//如果没有借阅则插入数据库记录
				btnService.add(userPhone,bookId,term);
				resp.setContentType("text/html; charset=utf-8");
				out = resp.getWriter();
				// 将JAVA对象给fastjson转换
				String json = JSON.toJSONString("200");
				out.write(json);
				out.flush();
				out.close();
			}else if(userPhoneNum <1){
				PrintWriter out = resp.getWriter();
				resp.setContentType("text/html; charset=utf-8");
				// 将JAVA对象给fastjson转换
				String json = JSON.toJSONString(402);
				out.write(json);
				out.flush();
				out.close();
			}else {
				PrintWriter out = resp.getWriter();
				resp.setContentType("text/html; charset=utf-8");
				// 将JAVA对象给fastjson转换
				String json = JSON.toJSONString(401);
				out.write(json);
				out.flush();
				out.close();
				
			}
			
		}
	}
	
	
	public static void main(String[] args) {
		
	}
}
